package com.tdkj.action.backstage.information;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;

import com.tdkj.action.BaseAction;
import com.tdkj.model.information.BeanBaseInfo;
import com.tdkj.model.information.BeanInfoModule;
import com.tdkj.model.information.BeanModuleColumn;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.information.IBeanBaseInfoService;
import com.tdkj.service.information.IBeanInfoModuleService;
import com.tdkj.util.JsonUtil;

public class PublishInfoAction  extends BaseAction{
	private long id;
	private String classTypeJSON;
	private String mn;
	private String mt;
	private String cn;
	private String ct;
	private String title;
	private String resource;
	private String varietyid;
	private String variety;
	private String content;
	private Map<String,String>prop=new HashMap<String,String>();
	private BeanInfoModule infoModule;
	private List<BeanInfoModule> infoModules=new ArrayList<BeanInfoModule>();
	private List<BeanModuleColumn> columns=new ArrayList<BeanModuleColumn>();
	private IBeanInfoModuleService infoModuleService;
	private IBeanBaseInfoService baseInfoService;
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getClassTypeJSON() {
		return classTypeJSON;
	}
	public void setClassTypeJSON(String classTypeJSON) {
		this.classTypeJSON = classTypeJSON;
	}
	public String getMn() {
		return mn;
	}
	public void setMn(String mn) {
		this.mn = mn;
	}
	public String getMt() {
		return mt;
	}
	public void setMt(String mt) {
		this.mt = mt;
	}
	public String getCn() {
		return cn;
	}
	public void setCn(String cn) {
		this.cn = cn;
	}
	public String getCt() {
		return ct;
	}
	public void setCt(String ct) {
		this.ct = ct;
	}
	public String getVarietyid() {
		return varietyid;
	}
	public void setVarietyid(String varietyid) {
		this.varietyid = varietyid;
	}
	public String getVariety() {
		return variety;
	}
	public void setVariety(String variety) {
		this.variety = variety;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getResource() {
		return resource;
	}
	public void setResource(String resource) {
		this.resource = resource;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public Map<String, String> getProp() {
		return prop;
	}
	public void setProp(Map<String, String> prop) {
		this.prop = prop;
	}
	public BeanInfoModule getInfoModule() {
		return infoModule;
	}
	public void setInfoModule(BeanInfoModule infoModule) {
		this.infoModule = infoModule;
	}
	public List<BeanInfoModule> getInfoModules() {
		return infoModules;
	}
	public void setInfoModules(List<BeanInfoModule> infoModules) {
		this.infoModules = infoModules;
	}
	public List<BeanModuleColumn> getColumns() {
		return columns;
	}
	public void setColumns(List<BeanModuleColumn> columns) {
		this.columns = columns;
	}
	@Resource
	public void setInfoModuleService(IBeanInfoModuleService infoModuleService) {
		this.infoModuleService = infoModuleService;
	}
	@Resource
	public void setBaseInfoService(IBeanBaseInfoService baseInfoService) {
		this.baseInfoService = baseInfoService;
	}
	//获取首页
	public String findAll(){
		assemblyJson();
		infoModules=infoModuleService.findAll(BeanInfoModule.class);
		return "index";
	}
	public String findAddPage(){
		mt=this.getMt();
		mn=this.getMn();
		ct=this.getCt();
		cn=this.getCn();
		String location=findLocation(mt,ct);
		return location+"_add";
	}
	private String findLocation(String mt2, String ct2) {
		// TODO Auto-generated method stub
		String location="page";
		if(mt.equals("1")){location="1page";}
		if(mt.equals("3")&&(ct.equals("3")||ct.equals("4"))){location="334page";}
		if(mt.equals("7")){location="7page";}
		return location;
	}
	//发布消息
	public String addInfo(){
		BeanBaseInfo info=new BeanBaseInfo();
		if(this.getTitle()==null||this.getTitle().equals("")) {
			this.addFieldError("titleVlidate", "标题不能为空");
		}
		if(this.getTitle()!=null)info.setTitle(this.getTitle());
		if(this.getContent()!=null)info.setContent(this.getContent());
		if(this.getMn()!=null)info.setModulename(this.getMn());
		if(this.getMt()!=null)info.setModuletype(this.getMt());
		if(this.getCn()!=null)info.setColumnname(this.getCn());
		if(this.getCt()!=null)info.setColumntype(this.getCt());
		if(this.getVariety()!=null)info.setVariety(this.getVariety());
		if(this.getVarietyid()!=null)info.setVarietyid(this.getVarietyid());
		if(this.getMt()!=null)mt=this.getMt();
		if(this.getCt()!=null)ct=this.getCt();
		if(this.getMn()!=null)mn=this.getMn();
		if(this.getCn()!=null)cn=this.getCn();
		if(this.getResource()!=null)info.setResource(this.getResource());
		info.setCreatetime(new Timestamp(System.currentTimeMillis()));
		info.setClicktime(1);
		info.setStatus("未审核");
		if(this.getProp()!=null){
			info.setProp(this.getProp());
		}
		//BeanUser u=this.getCurrentUser();
		//info.setUser(u);
		baseInfoService.save(info);
		return findAddPage();
	}
	public void assemblyJson(){
		String classTypeJSON="";
		List<BeanInfoModule> list=new ArrayList<BeanInfoModule>();
		list=infoModuleService.findAll(BeanInfoModule.class);
		list=substringList(list);
		JsonConfig config = new JsonConfig();
		config.setIgnoreDefaultExcludes(false);
		config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
		classTypeJSON = JSONSerializer.toJSON(list, config).toString();
		String[] oldStr={"moduleColumns","id","modulename","moduletype","columnname","columntype"};
		String[] newStr={"chils","old","name","id","name","id"};
		classTypeJSON=JsonUtil.replaceProperty(classTypeJSON, oldStr, newStr);
		this.setClassTypeJSON(classTypeJSON);
	}
	private List<BeanInfoModule> substringList(List<BeanInfoModule> ol){
		List<BeanInfoModule> nl=new ArrayList<BeanInfoModule>();
		List<BeanInfoModule> templ=new ArrayList<BeanInfoModule>();
		for(int i=0;i<ol.size();i++){
			BeanInfoModule mb=ol.get(i);
			if(mb.getModuletype().equals("6")||mb.getModuletype().equals("9")
					||mb.getModuletype().equals("11")||mb.getModuletype().equals("14")){
				//
			}else{
				templ.add(mb);
			}
		}
		return templ;
	}
}
